From 86df734da739efd4760bc47e34fea7c2340c6c06 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Tue, 9 May 2023 07:12:32 -0600 Subject: [PATCH] datetime purity. (#1102) eliminate some QDateTime -> gpsbabel:DateTime conversions. These can also help performance as DateTime defaults to UTC. eliminate some unecessary involvment of the UNIX epoch when computing time differences. --- gpx.cc | 8 +++----- interpolate.cc | 14 +++++++------- interpolate.h | 4 +--- kml.cc | 4 ++-- position.cc | 22 ++++++++++++---------- position.h | 6 ++++-- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/gpx.cc b/gpx.cc index 246e709af..7b499a7d6 100644 --- a/gpx.cc +++ b/gpx.cc @@ -437,7 +437,7 @@ xml_parse_time(const QString& dateTimeString) } int year = 0, mon = 0, mday = 0, hour = 0, min = 0, sec = 0; - QDateTime dt; + gpsbabel::DateTime dt; int res = sscanf(timestr, "%d-%d-%dT%d:%d:%d", &year, &mon, &mday, &hour, &min, &sec); if (res > 0) { @@ -452,8 +452,6 @@ xml_parse_time(const QString& dateTimeString) // Any offsets that were stuck at the end. dt = dt.addSecs(-off_sign * off_hr * 3600 - off_sign * off_min * 60); - } else { - dt = QDateTime(); } return dt; } @@ -461,7 +459,7 @@ xml_parse_time(const QString& dateTimeString) void GpxFormat::gpx_end(QStringView /*unused*/) { - static QDateTime gc_log_date; + static gpsbabel::DateTime gc_log_date; // Remove leading, trailing whitespace. cdatastr = cdatastr.trimmed(); @@ -568,7 +566,7 @@ GpxFormat::gpx_end(QStringView /*unused*/) (!wpt_tmp->gc_data->last_found.isValid())) { wpt_tmp->AllocGCData()->last_found = gc_log_date; } - gc_log_date = QDateTime(); + gc_log_date = gpsbabel::DateTime(); break; case tt_cache_favorite_points: wpt_tmp->AllocGCData()->favorite_points = cdatastr.toInt(); diff --git a/interpolate.cc b/interpolate.cc index f61ff6fab..09fcebc54 100644 --- a/interpolate.cc +++ b/interpolate.cc @@ -19,16 +19,18 @@ */ +#include "interpolate.h" + #include // for INT_MAX #include // for abs, ceil, isfinite, round #include // for abs, strtod #include // for optional #include // for QString -#include // for qAsConst, QAddConst<>::Type +#include // for qint64, qAsConst, qRound64 #include "defs.h" -#include "interpolate.h" +#include "formspec.h" // for FormatSpecificDataList #include "grtcirc.h" // for linepart, RAD, gcdist, radtomiles #include "src/core/datetime.h" // for DateTime #include "src/core/logging.h" // for Fatal @@ -81,8 +83,7 @@ void InterpolateFilter::process() } else { std::optional timespan; if (wpt->creation_time.isValid() && time1.isValid()) { - timespan = wpt->creation_time.toMSecsSinceEpoch() - - time1.toMSecsSinceEpoch(); + timespan = time1.msecsTo(wpt->creation_time); } std::optional altspan; if (altitude1 != unknown_alt && wpt->altitude != unknown_alt) { @@ -121,8 +122,7 @@ void InterpolateFilter::process() wpt_new->shortname = QString(); wpt_new->description = QString(); if (timespan.has_value()) { - wpt_new->SetCreationTime(0, time1.toMSecsSinceEpoch() + - round(frac * *timespan)); + wpt_new->SetCreationTime(time1.addMSecs(qRound64(frac * *timespan))); } else { wpt_new->creation_time = gpsbabel::DateTime(); } @@ -152,7 +152,7 @@ void InterpolateFilter::process() lat1 = wpt->latitude; lon1 = wpt->longitude; altitude1 = wpt->altitude; - time1 = wpt->creation_time; + time1 = wpt->creation_time.toUTC(); // use utc to avoid tz conversions. } } backuproute.flush(); diff --git a/interpolate.h b/interpolate.h index 5aabe1aa2..5df73a8ea 100644 --- a/interpolate.h +++ b/interpolate.h @@ -22,10 +22,8 @@ #ifndef INTERPOLATE_H_INCLUDED_ #define INTERPOLATE_H_INCLUDED_ -#include // for optional - +#include // for QString #include // for QVector -#include // for qint64 #include "defs.h" // for ARG_NOMINMAX, arglist_t, ARGTYPE_BEGIN_EXCL, ARG... #include "filter.h" // for Filter diff --git a/kml.cc b/kml.cc index 8683887ac..f05d86aa2 100644 --- a/kml.cc +++ b/kml.cc @@ -354,8 +354,8 @@ void KmlFormat::wr_init(const QString& fname) { char u = 's'; waypt_init_bounds(&kml_bounds); - kml_time_min = QDateTime(); - kml_time_max = QDateTime(); + kml_time_min = gpsbabel::DateTime(); + kml_time_max = gpsbabel::DateTime(); if (opt_units) { u = tolower(opt_units[0]); diff --git a/position.cc b/position.cc index 4d2d0695d..b99f55de3 100644 --- a/position.cc +++ b/position.cc @@ -19,15 +19,17 @@ */ -#include // for fabs -#include // for strtod +#include "position.h" + +#include // for abs +#include // for strtod -#include // for QList -#include // for qAsConst, QAddConst<>::Type +#include // for QList +#include // for qAsConst, qRound64, qint64 #include "defs.h" -#include "grtcirc.h" // for RAD, gcdist, radtometers -#include "position.h" +#include "grtcirc.h" // for RAD, gcdist, radtometers +#include "src/core/datetime.h" // for DateTime #if FILTERS_ENABLED @@ -64,7 +66,7 @@ void PositionFilter::position_runqueue(WaypointList* waypt_list, int qtype) if (dist <= pos_dist) { if (check_time) { - double diff_time = fabs(waypt_time(qlist.at(i).wpt) - waypt_time(qlist.at(j).wpt)); + qint64 diff_time = std::abs(qlist.at(j).wpt->creation_time.msecsTo(qlist.at(i).wpt->creation_time)); if (diff_time >= max_diff_time) { continue; } @@ -156,13 +158,13 @@ void PositionFilter::process() void PositionFilter::init() { - char* fm; pos_dist = 0.0; - max_diff_time = 0.0; + max_diff_time = 0; check_time = false; if (distopt != nullptr) { + char* fm; pos_dist = strtod(distopt, &fm); if (!((*fm == 'm') || (*fm == 'M'))) { @@ -173,7 +175,7 @@ void PositionFilter::init() if (timeopt != nullptr) { check_time = true; - max_diff_time = strtod(timeopt, &fm); + max_diff_time = qRound64(strtod(timeopt, nullptr) * 1000.0); } } diff --git a/position.h b/position.h index a88348da1..0a51d9805 100644 --- a/position.h +++ b/position.h @@ -22,7 +22,9 @@ #ifndef POSITION_H_INCLUDED_ #define POSITION_H_INCLUDED_ -#include // for QVector +#include // for QString +#include // for QVector +#include // for qint64 #include "defs.h" // for route_head (ptr only), ARG_NOMINMAX, ARGTYPE_FLOAT #include "filter.h" // for Filter @@ -43,7 +45,7 @@ private: route_head* cur_rte = nullptr; double pos_dist{}; - double max_diff_time{}; + qint64 max_diff_time{}; char* distopt = nullptr; char* timeopt = nullptr; char* purge_duplicates = nullptr; -- 2.30.2